#include <linux/module.h>
#include <linux/init.h>
#include <linux/moduleparam.h>
#include <linux/fs.h>
#include <linux/kdev_t.h>

static int major = 0;
static int minor = 0;

module_param(major,int,S_IRUGO);
module_param(minor,int,S_IRUGO);

static dev_t dev_num;

static int param_init(void)
{
    int ret = 0;
    if (major)
    {
        dev_num = MKDEV(major,minor);
        printk("major is %d\n",major);
        printk("minor is %d\n",minor);
        ret = register_chrdev_region(dev_num,1,"chrdev_name");
        if (ret < 0)
        {
            printk("register_chrdev_region error\n");
        }
        printk("register_chrdev_region ok\n");
    }else
    {
        ret = alloc_chrdev_region(&dev_num,0,1,"chrdev_num");
        if (ret < 0)
        {
            printk("alloc_chrdev_region error\n");
        }
        printk("alloc_chrdev_region ok\n");
        major = MAJOR(dev_num);
        minor = MINOR(dev_num);
        printk("major is %d\n",major);
        printk("minor is %d\n",minor);
    }
    return 0;
}

static void param_exit(void)
{
    unregister_chrdev_region(dev_num,1);
    printk("module bye!\n");
}

module_init(param_init);
module_exit(param_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("yan");
MODULE_VERSION("v0.1");










